package com.easy.module.system.service.impl;

import com.easy.framework.common.constant.Constants;
import com.easy.framework.common.utils.Func;
import com.easy.module.system.service.IEmailService;
import com.easy.module.system.service.ISysConfService;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

@RequiredArgsConstructor
@Service
public class QQEmailServiceImpl implements IEmailService {

    private final ISysConfService sysConfService;

    private final JavaMailSender mailSender;

    private final String SYS_MAIL_EMAIL = "sys.mail.email";

    @Override
    public boolean support(String type) {
        return Func.isEmpty(type) || Constants.MAIL_QQ.equals(type);
    }

    /**
     * 发送邮件
     *
     * @param to
     * @param subject
     * @param content
     */
    public void send(String to, String subject, String content) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sysConfService.getValue(SYS_MAIL_EMAIL));
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
